﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCPhuCapChucVu : System.Web.UI.UserControl
    {

        NSPhuCapChucVu phucap = new NSPhuCapChucVu();
        private List<NSPhuCapChucVu> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            cbbPhuCapChucVu.SelectedItem.Value = string.Empty;
            dtNgayHetHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
            dtNgayHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtGhiChu.Text = string.Empty;
            txtHeSo.Text = string.Empty;
        }
        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {
            storePhuCapChucVu.DataSource = common.LoadDataCommonByCodeKey(common.PhuCapChucVu);
            storePhuCapChucVu.DataBind();
            hdNhanVienId.Text = nhanvienID;
            hdPhucCapId.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdPhuCapChucVu.Title = "Tạo mới phụ cấp chức vụ";
                ResetControl();
                hdType.Text = type;
                btnSavePCCV.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdPhuCapChucVu.Title = "Sửa đổi phụ cấp";
                hdType.Text = type;
                btnSavePCCV.Hidden = false;
                LoadDataPCCV(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdPhuCapChucVu.Title = "Xem chi tiết đổi phụ cấp";
                btnSavePCCV.Hidden = true;
                hdType.Text = type;
                LoadDataPCCV(Convert.ToInt16(hdId));
            }
            wdPhuCapChucVu.Show();
        }
        public void LoadDataPCCV(int hdId)
        {
            lst = (List<NSPhuCapChucVu>)Session["PCCV"];
            NSPhuCapChucVu item = lst.Where(a => a.PhuCapId.Equals(hdId)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.NgayHieuLuc != null ? ((DateTime)item.NgayHieuLuc).ToString("dd/MM/yyyy") : string.Empty;
            dtNgayHetHieuLuc.Text = item.NgayHetHieuLuc.HasValue ? item.NgayHetHieuLuc.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtGhiChu.Text = item.GhiChu;
            txtHeSo.Text = item.HeSoPhuCap.ToString();
            cbbPhuCapChucVu.SelectedItem.Value = item.LoaiPhuCapChucVuID.HasValue && item.LoaiPhuCapChucVuID.Value > 0 ? item.LoaiPhuCapChucVuID.ToString() : string.Empty;
        }
        protected void btnSavePCCV_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayHieuLuc = Convert.ToDateTime(dtNgayHieuLuc.Text);
            DateTime ngayHetHieuLuc = Convert.ToDateTime(dtNgayHetHieuLuc.Text);
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;
            int loaiPhuCap = !string.IsNullOrEmpty(cbbPhuCapChucVu.SelectedItem.Value) ? Convert.ToInt16(cbbPhuCapChucVu.SelectedItem.Value) : 0;
            decimal heSo = !string.IsNullOrEmpty(txtHeSo.Text) ? Convert.ToDecimal(txtHeSo.Text) : 0;
            if (hdType.Text.Equals("AddNew"))
            {
                NSPhuCapChucVu item = nhanvien.GetPhuCapChucVu(nhanVienId, heSo, loaiPhuCap, ngayHieuLuc, ngayHetHieuLuc, txtGhiChu.Text, cbbPhuCapChucVu.SelectedItem.Text);
                ((UCNhanVien)this.Parent).GetPhuCapChucVu(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                phucap.EditPCCV(Convert.ToInt16(hdPhucCapId.Text), heSo, loaiPhuCap, ngayHieuLuc, ngayHetHieuLuc, txtGhiChu.Text, cbbPhuCapChucVu.SelectedItem.Text);
                ((UCNhanVien)this.Parent).ReloadGrid("PCCV");
            }
            X.Mask.Hide();

            wdPhuCapChucVu.Hide();

        }
    }
}